package com.ruoyi.bond.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.bond.domain.*;
import com.ruoyi.bond.service.IBondZhCovService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 可转债数据一览表Controller
 *
 * @author leng.xz
 * @date 2024-05-17
 */
@Controller
@RequestMapping("/bond/ZhCov")
public class BondZhCovController extends BaseController
{
    private String prefix = "bond/ZhCov";

    @Autowired
    private IBondZhCovService bondZhCovService;

    @RequiresPermissions("bond:ZhCov:view")
    @GetMapping()
    public String ZhCov()
    {
        return prefix + "/ZhCov";
    }

    /**
     * 查询可转债数据一览表列表
     */
    @RequiresPermissions("bond:ZhCov:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BondZhCov bondZhCov)
    {
        startPage();
        List<BondZhCov> list = bondZhCovService.selectBondZhCovList(bondZhCov);
        return getDataTable(list);
    }

    /**
     * 导出可转债数据一览表列表
     */
    @RequiresPermissions("bond:ZhCov:export")
    @Log(title = "可转债数据一览表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BondZhCov bondZhCov)
    {
        List<BondZhCov> list = bondZhCovService.selectBondZhCovList(bondZhCov);
        ExcelUtil<BondZhCov> util = new ExcelUtil<BondZhCov>(BondZhCov.class);
        return util.exportExcel(list, "ZhCov");
    }

    /**
     * 新增可转债数据一览表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存可转债数据一览表
     */
    @RequiresPermissions("bond:ZhCov:add")
    @Log(title = "可转债数据一览表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BondZhCov bondZhCov)
    {
        return toAjax(bondZhCovService.insertBondZhCov(bondZhCov));
    }

    /**
     * 修改可转债数据一览表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BondZhCov bondZhCov = bondZhCovService.selectBondZhCovById(id);
        mmap.put("bondZhCov", bondZhCov);
        return prefix + "/edit";
    }

    /**
     * 修改保存可转债数据一览表
     */
    @RequiresPermissions("bond:ZhCov:edit")
    @Log(title = "可转债数据一览表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BondZhCov bondZhCov)
    {
        return toAjax(bondZhCovService.updateBondZhCov(bondZhCov));
    }

    /**
     * 删除可转债数据一览表
     */
    @RequiresPermissions("bond:ZhCov:remove")
    @Log(title = "可转债数据一览表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bondZhCovService.deleteBondZhCovByIds(ids));
    }

    /**
     * 查询可转债概况
     */
    @RequiresPermissions("bond:ZhCov:list")
    @GetMapping("/summary/{id}")
    public String summary(@PathVariable("id") String id, ModelMap mmap) {
        List<BondCbSummarySina> bondSummary = bondZhCovService.selectBondSummaryById(id);
        mmap.put("bondSummary", bondSummary);
        List<BondCbProfileSina> bondProfile = bondZhCovService.selectBondProfileById(id);
        mmap.put("bondProfile", bondProfile);
        return prefix + "/summary";
    }


    /**
     * 查询可转债行情
     */
    @RequiresPermissions("bond:ZhCov:list")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap) {
        BondZhHsCovSpot bondCovSpot = bondZhCovService.selectBondCovSpotById(id);
        mmap.put("bondCovSpot", bondCovSpot);
        List<BondZhHsCovDaily> bondCovDaliy = bondZhCovService.selectBondCovDailyById(id);

        JSONArray price =  new JSONArray();
        JSONArray volumes =  new JSONArray();
        List<String> jsonArray = new ArrayList();
        DateTimeFormatter formatterFrom = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
        DateTimeFormatter formatterTo = DateTimeFormatter.ofPattern("yyyyMMdd");
        bondCovDaliy.forEach(item -> {
            jsonArray.add(LocalDateTime.parse(item.getField0(), formatterFrom).format(formatterTo));
            //// 开盘，收盘，最低，最高
            price.add(new double[]{item.getField1().doubleValue(),item.getField4().doubleValue(),item.getField3().doubleValue(),item.getField2().doubleValue(),item.getField5().doubleValue()});
            volumes.add(item.getField5().doubleValue());
        });
        mmap.put("dates",  jsonArray);
        mmap.put("price", price);
        mmap.put("volumes", volumes);
        return prefix + "/detail";
    }

}
